<?php
/**
 * Created by PhpStorm.
 * User: Liulibao
 * Date: 2019/11/19
 * Time: 21:34
 */

namespace app\common\model;


use think\Model;

class InquiryInfoModel extends Model
{
    protected $table = 'ds_inquiry_info';

    public $page_info;

    protected $pageSize = 10;

    public static $goodConfig = array(
        1 => '低配置',
        2 => '中配置',
        3 => '高配置',
    );

    /**
     * @description: 获取列表  @MrLiu
     * @param array $condition
     * @param $page
     * @param string $order
     * @return false|\PDOStatement|string|\think\Collection
     * @internal param bool $isArray
     */
    public function getInquiryList($condition = [], $page = 0, $order = 'info.id desc')
    {
        $condition['info.is_del'] = 0;

        $result = db('inquiry_info')->alias('info');

        if (!empty($condition) && is_array($condition)) {
            $result = $result->where($condition);
        }

        if ($page) {
            $result = $result
                ->field('info.*,goodscommon.goods_name,goodscommon.gc_name,goodscommon.store_name,inquiries.inquiry_name,inquiry_station.station_name')
                ->join('goodscommon', 'goodscommon.goods_commonid=info.goods_id', 'LEFT')
                ->join('inquiries', 'inquiries.id=info.inquiry_id', 'LEFT')
                ->join('inquiry_station', 'inquiry_station.station_id=info.station_id', 'LEFT')
                ->order($order)
                ->paginate($page, false, ['query' => request()->param()]);

            $this->page_info = $result;

            return $result->items();
        } else {
            return $result->order($order)->select();
        }
    }

    /**
     * @description: 运营商获取列表  @MrLiu
     * @param array $condition
     * @param $page
     * @param string $order
     * @return false|\PDOStatement|string|\think\Collection
     * @internal param bool $isArray
     */
    public function getSellerInquiryList($condition = [], $page = 0, $order = 'id desc')
    {
        if ($page) {
            $res= db('seller_inquiry')->alias('s')
                  ->join('inquiries i','i.id=s.inquiry_id')
                  ->where($condition)->field('s.*,i.inquiry_name')->order($order)->paginate($page,false,['query' => request()->param()]);
            $this->page_info=$res;
            return $res->items();
        } else {
            return db('seller_inquiry')->where($condition)->field($field)->order($order)->select();
        }
    }

    /**
     * @description:   @MrLiu
     * @param array $condition
     * @param $page
     * @param string $field
     * @return array
     */
    public function getList($condition = [])
    {
        $conditions = !empty($condition) ? $condition : array('id' => array('gt', '0'));
        $conditions['is_del'] = 0;
        return $this->where($conditions)
            ->paginate($this->pageSize)
            ->toArray();
    }

    /**
     * @description: 获取某一条数据  @MrLiu
     * @param $id
     * @return array|bool|false|\PDOStatement|string|Model
     */
    public function getOneById($id)
    {
        if (intval($id) <= 0) {
            return false;
        }
        return $this->where(array('id' => $id, 'is_del' => 0))->find();
    }

    /**
     * @description: 根据条件查询获取某一条数据 @MrLiu
     * @param array $condition
     * @return array|bool|false|\PDOStatement|string|Model
     */
    public function getOne($condition = [])
    {
        if (empty($condition)) {
            return false;
        }
        $condition['is_del'] = 0;
        return $this->where($condition)->find();
    }

    /**
     * @description: 新增数据  @MrLiu
     * @param $data
     * @return int|string
     */
    public function add($data)
    {
        $data['created_at'] = date('Y-m-d H:i:s', time());
        return $this->insertGetId($data);
    }

    /**
     * @description: 更新信息  @MrLiu
     * @param $data
     * @param $ac_id
     * @return $this
     */
    public function edit($data,$id)
    {
        return $this->where('id',$id)->update($data);
    }

    /**
     * @description: 删除分类   @MrLiu
     * @param $id
     * @return $this
     */
    public function del($id)
    {
         $data['is_del']='1';
        return db('inquiries')->where('id', $id)->update($data);
    }
}